package com.wyth.lottery.utils;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.SoundEffectConstants;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.wyth.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangbo on 2017/9/5
 */

public class RandomUtils {

    static ArrayList<String> random = new ArrayList<>();  // 用于存放所取的值的数组

    public static ArrayList<String> creatRandom(int conut, int numconut) {

        random.clear();

        int number = conut;// 控制随机数产生的范围

        List arr = new ArrayList();
        for (int i = 0; i < conut; i++)
            arr.add(i + 1);// 为ArrayList添加元素

        for (int j = 0; j < numconut; j++) {
            int index = (int) (Math.random() * number);// 产生一个随机数作为索引
            int i = (int) arr.get(index);
            String s = i + "";
            random.add(s);
            arr.remove(index);// 移除已经取过的元素
            number--;// 将随机数范围缩小1
        }

        return random;
    }


    public static ArrayList<String> creatSDRandom(int conut, int numconut) {

        random.clear();

        int number = conut;// 控制随机数产生的范围

        List arr = new ArrayList();
        for (int i = 0; i < conut; i++)
            arr.add(i);// 为ArrayList添加元素

        for (int j = 0; j < numconut; j++) {
            int index = (int) (Math.random() * number);// 产生一个随机数作为索引
            int i = (int) arr.get(index);
            String s = i + "";
            random.add(s);
            arr.remove(index);// 移除已经取过的元素
            number--;// 将随机数范围缩小1
        }

        return random;
    }

    /**
     * 计算器算法
     *
     * @param textView  按钮
     * @param mlist     用来存储数据的集合
     * @param tv_number 用来显示的一个textView
     */
    public static boolean mToastIsShow = false;

    public static void SetOnClickListener(final TextView textView, final List<String> mlist, final TextView tv_number) {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = textView.getText().toString();
                if (mlist.size() == 0 && s1.equals("0")) {

                } else {
                    if (mlist.size() >= 2) {
                        //自定义Toast
                        Toast toast = new Toast(MyApplication.getContext());
                        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.select_toast_layout, null);
                        toast.setView(view);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setDuration(Toast.LENGTH_SHORT);
                        if (!mToastIsShow) {
                            mToastIsShow = true;
                            toast.show();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        Thread.sleep(3000);
                                        mToastIsShow = false;
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }).start();
                        }
                    } else {
                        String s = textView.getText().toString();
                        mlist.add(s);
                        getString(mlist, tv_number);


                    }
                }

            }

        });

    }

    private static void aa(List<String> mlist, TextView tv_number, TextView textView) {
        if (mlist.size() >= 2) {
            Toast.makeText(MyApplication.getContext(), "只能选择2-99倍", Toast.LENGTH_SHORT).show();
            getString(mlist, tv_number);
        } else {
            String s = textView.getText().toString();
            if (mlist.size() == 0 && s.equals("0")) {

            } else {
                mlist.add(s);
                getString(mlist, tv_number);
            }

        }
    }

    public static void getString(List<String> mlist, TextView tv_number) {
        StringBuffer buffer = new StringBuffer();
        for (String ss : mlist) {
            buffer.append(ss);
        }
        String s = buffer.toString();
        int i = Integer.parseInt(s);
        if (i <= 0) {
            i = 1;
        }
        tv_number.setText(String.valueOf(i));
    }
}
